package com.zhp.ggweather;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.location.LocationClient;
import com.zhp.ggweather.adapter.WeatherPagerAdapter;
import com.zhp.ggweather.app.App;
import com.zhp.ggweather.db.DataBase;
import com.zhp.ggweather.fragment.FirstWeatherFragment;
import com.zhp.ggweather.fragment.SecondWeatherFragment;
import com.zhp.ggweather.util.NetBroadcastReceiver.EventHandler;
import com.zhp.ggweather.util.SharePreferenceUtil;

public class WeatherFragment extends Fragment implements OnClickListener,EventHandler{

	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getActivity().getActionBar().hide();
		
	}
	@SuppressLint("InflateParams")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v=inflater.inflate(R.layout.layout_weather, null);
		
		
		initWidgets(v);
//		getWeather();
		return v;
		
		
	}

	
	public static final String UPDATE_WIDGET_WEATHER_ACTION = "com.way.action.update_weather";
	private static final int LOACTION_OK = 0;
	private static final int UPDATE_EXISTS_CITY = 2;
	
	private View mWelcomeView;

	public static final int GET_WEATHER_SCUESS = 3;
	public static final int GET_WEATHER_FAIL = 4;

	private ViewPager mViewPager;

	private long startTime;

	private App mApp;

	private DataBase mDataBase;
	
	private SharePreferenceUtil mSharePreferenceUtil;

	private ImageView mImageView,mProjectionTitleCityManager,
	mProjectionTitleShare,mProjectionTitleLocation,mProjectionTitleUpdateProgress,mWeatherItemFirstImg,
	mWeatherItemFirstWeatherImg;

	private TextView mProjectionTitleCityName,mWeatherItemFirstCity,mWeatherItemFirstTime,mWeatherItemFirstPM,

	mWeatherItemFirstPMData,mWeatherItemFirstPmQuality,mWeatherItemFirstWeekToday,mWeatherItemFirstTemperature,

	mWeatherItemFirstClimate,mWeatherItemFirstWind;

	private List<Fragment> fragments;

	private WeatherPagerAdapter mWeatherPagerAdapter;

	private LocationClient mLocationClient;


	

	private void initWidgets(View v){
		
		mWelcomeView=v.findViewById(R.id.activity_main_welcome_layout_view);
		mImageView=(ImageView) mWelcomeView.findViewById(R.id.welcome_layout_image);
		mImageView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				mWelcomeView.setVisibility(View.GONE);
			}
		});
		
		
		mProjectionTitleCityManager=(ImageView) v.findViewById(R.id.projection_title_city_manager);
				
		mProjectionTitleCityManager.setOnClickListener(this);

		mProjectionTitleShare=(ImageView) v.findViewById(R.id.projection_title_share);
		mProjectionTitleShare.setOnClickListener(this);

		mProjectionTitleCityName=(TextView) v.findViewById(R.id.projection_title_city_name);

		mProjectionTitleLocation=(ImageView) v.findViewById(R.id.projection_title_location);
		mProjectionTitleLocation.setOnClickListener(this);

		mProjectionTitleUpdateProgress=(ImageView) v.findViewById(R.id.projection_title_update_progress);
		mProjectionTitleUpdateProgress.setOnClickListener(this);

		mViewPager=(ViewPager) v.findViewById(R.id.activity_main_viewpager);
		fragments = new ArrayList<Fragment>();
		fragments.add(new FirstWeatherFragment());
		fragments.add(new SecondWeatherFragment());

		mWeatherPagerAdapter = new WeatherPagerAdapter(getActivity().getSupportFragmentManager(), fragments);
		mViewPager.setAdapter(mWeatherPagerAdapter);
		((CirclePageIndicator) v.findViewById(R.id.activity_main_indicator)).setViewPager(mViewPager);;

		mWeatherItemFirstCity=(TextView) v.findViewById(R.id.weather_item_first_city);
		mWeatherItemFirstClimate=(TextView) v.findViewById(R.id.weather_item_first_climate);
		mWeatherItemFirstImg=(ImageView) v.findViewById(R.id.weather_item_first_pm2_5_img);
		mWeatherItemFirstPM=(TextView) v.findViewById(R.id.weather_item_first_pm2_5);
		mWeatherItemFirstPMData=(TextView) v.findViewById(R.id.weather_item_first_pm_data);
		mWeatherItemFirstPmQuality=(TextView) v.findViewById(R.id.weather_item_first_pm2_5_quality);
		mWeatherItemFirstTemperature=(TextView) v.findViewById(R.id.weather_item_first_temperature);
		mWeatherItemFirstTime=(TextView) v.findViewById(R.id.weather_item_first_time);
		mWeatherItemFirstWeatherImg=(ImageView) v.findViewById(R.id.weather_item_first_weather_img);
		mWeatherItemFirstWeekToday=(TextView)v. findViewById(R.id.weather_item_first_week_today);
		mWeatherItemFirstWind=(TextView) v.findViewById(R.id.weather_item_first_wind);

	}
//
//	@SuppressLint("HandlerLeak")
//	private Handler mHandler = new Handler() {
//		public void handleMessage(Message msg) {
//			switch (msg.what) {
//			case LOACTION_OK:
//				break;
//			case UPDATE_EXISTS_CITY:
//				String sPCityName = mSharePreferenceUtil.getCity();
//				updateWeather(mDataBase.getCity(sPCityName));
//				break;
//			case GET_WEATHER_SCUESS:
//				break;
//			case GET_WEATHER_FAIL:
//				break;
//			default:
//				break;
//			}
//		}
//
//	};
//	private void updateWeather(City city) {
//		// 因为在异步任务中有判断网络连接问题，在网络请求前先获取文件中的缓存，所以，此处未处理网络连接问题
//		if (city == null) {
//			MyToast.showLong(mApp, "未找到此城市,请重新定位或选择...");
//			return;
//		}
////		timeTv.setText("同步中...");
////		mProjectionTitleCityName.setText(city.getName());
//		mProjectionTitleCityName.setText("beijing");
////		mProjectionTitleUpdateProgress.startAnim();
//		new ObtainWeather(mHandler, city).execute();
//	}
//	
//	private void getWeather(){
//		startTime=System.currentTimeMillis();
//		NetBroadcastReceiver.mListeners.add(this);
//		mApp=App.getInstance();
//		System.out.println("=====>"+mApp);
//		mSharePreferenceUtil=mApp.getSharePreferenceUtil();
//		mLocationClient=mApp.getLocationClient();
//		mLocationClient.registerLocationListener(new BDLocationListener() {
//
//			@Override
//			public void onReceivePoi(BDLocation arg0) {
//				// TODO Auto-generated method stub
//
//			}
//
//			@Override
//			public void onReceiveLocation(BDLocation arg0) {
//				// TODO Auto-generated method stub
//
//			}
//		});
//
//
//
//	}
//
//
//	@Override
//	public void onClick(View v) {
//		// TODO Auto-generated method stub
//
//	}
//
//	@Override
//	public void onNetChange() {
//		// TODO Auto-generated method stub
//		if (NetUtil.getNetworkState(getActivity()) == NetUtil.NETWORK_NONE) {
//			MyToast.showLong(getActivity(), R.string.net_err);
//		} else {
//			if (!TextUtils.isEmpty(mSharePreferenceUtil.getCity()))
//				mHandler.sendEmptyMessage(UPDATE_EXISTS_CITY);
//		}
//	}

	@Override
	public void onNetChange() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}
}
